var code                = '';

function checkUsername(Nusername){
    if(Nusername === '' || Nusername === 0){
        return 'The Nusername cannot be empty';
    }else if(Nusername.length<3){
        return 'The length of the username must be longer than 5';
    }
    return '';
}

$(function(){
    //点击获得验证码按钮设置
    $('#getCode').on('click',function(e){
        e.preventDefault();
        var email       = $('#email').val();
        if(email !== ''){
            $.ajax({
                type    : 'POST',
                url     : 'http://localhost:8080/verification',
                data    :{
                    email   : email,
                    getCode    : 'Get a verification code'
                },
                success     :function (data){
                    code    = data;
                    console.log('data ='+data);
                },
                error       :function(errorMsg){
                    console.log(errorMsg);
                }
            });
        }else{
            $('#error-msg').text('The email can not be empty');
        }
    })

    $('#registerOnly').on('click',function(e){
        e.preventDefault();
        var token = $('#token').val()+'\r\n';
        //如果
        if(code !== ''){
            if(code === token){
                $.ajax({
                    type    : 'POST',
                    url     : 'http://localhost:8080/register',
                    data    :{
                        username            : $('#username').val(),
                        password            : $('#password').val(),
                        repeatPassword     : $('#repeatPassword').val(),
                        firstname           : $('#firstname').val(),
                        lastname            : $('#lastname').val(),
                        email               : $('#email').val(),
                        token               : $('#token').val(),
                        country             : $('#country').val(),
                        state               : $('#state').val(),
                        city                : $('#city').val(),
                        address1            : $('#address1').val(),
                        address2            : $('#address2').val(),
                        zip                 : $('#zip').val(),
                        phone               : $('#phone').val(),
                        favcategory         : $('#favcategory').val(),
                        registerOnly        : 'registerOnly'
                    },
                    success     :function (data){
                        if(data.indexOf("address") !== -1){
                            window.location.href='http://localhost:8080/' +data.substring(data.indexOf(":"+1),data.length);
                            console.log('add:'+address);
                        }else{
                            $('#error-msg').text(data);
                        }
                        console.log('register ='+data);
                    },
                    error       :function(errorMsg){
                        console.log(errorMsg);
                    }
                });
            }else{
                $('#error-msg').text('The verification code is wrong');
            }
        }else{
            $('#error-msg').text('Please get a verification code first');
        }

    })

    $('#registerAndLogin').on('click',function(e){
        e.preventDefault();
        var token = $('#token').val()+'\r\n';
        //
        if(code !== ''){
            if(code === token){
                $.ajax({
                    type    : 'POST',
                    url     : 'http://localhost:8080/register',
                    data    :{
                        username            : $('#username').val(),
                        password            : $('#password').val(),
                        repeatPassword     : $('#repeatPassword').val(),
                        firstname           : $('#firstname').val(),
                        lastname            : $('#lastname').val(),
                        email               : $('#email').val(),
                        token               : $('#token').val(),
                        country             : $('#country').val(),
                        state               : $('#state').val(),
                        city                : $('#city').val(),
                        address1            : $('#address1').val(),
                        address2            : $('#address2').val(),
                        zip                 : $('#zip').val(),
                        phone               : $('#phone').val(),
                        favcategory         : $('#favcategory').val(),
                        registerAndLogin        : 'registerAndLogin'
                    },
                    success     :function (data){
                        //有返回地址
                        if(data.indexOf("address") !== -1){
                            window.location.href='http://localhost:8080/' + data.substring(data.indexOf(":") + 1, data.length);
                        }else{
                            $('#error-msg').text(data);
                            console.log('register ='+data);
                        }
                    },
                    error       :function(errorMsg){
                        console.log(errorMsg);
                    }
                });
            }else{
                $('#error-msg').text('The verification code is wrong');
                console.log(code);
                console.log(token);

            }
        }
        else {
            $('#error-msg').text('Please get a verification code first');
        }

    })
    $('#username').on('blur',function(){
        var username    = $(this).val();
        var isValidate  = checkUsername(username);
        if(isValidate !== ''){
            $('#error-msg').text(isValidate);
        }else{
            $('#error-msg').text('');
        }
    });
    $('#password').on('blur',function(){
        var password    = $(this).val();
        if(password === ''){
            $('#error-msg').text('The password cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    });
    $('#repeatPassword').on('blur',function(){
        var repeatePassword    = $(this).val();
        var password    = $('#password').val();
        if(repeatePassword === ''){
            $('#error-msg').text('The repeat password cannot be empty');
        }else if(repeatePassword !== password){
            $('#error-msg').text('The password is not the same between the two entries');
        }else{
            $('#error-msg').text('');
        }
    });

    $('#firstname').on('blur',function(){
        var firstname    = $(this).val();
        if(firstname === ''){
            $('#error-msg').text('The firstname cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    });
    $('#lastname').on('blur',function(){
        var lastname    = $(this).val();
        if(lastname === ''){
            $('#error-msg').text('The lastname cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    });

    $('#email').on('blur',function(){
        var email    = $(this).val();
        if(email === ''){
            $('#error-msg').text('The email cannot be empty');
        }else if(email.indexOf('@') === -1){
            $('#error-msg').text('Please enter a reasonable email');
        }else{
            $('#error-msg').text('');
        }
    });

    $('#token').on('blur',function(){
        var token    = $(this).val();
        //检测正确性要提交之后。
        if(code === ''){
            $('#error-msg').text('Please get a verification code first');
        }else if(token === ''){
            $('#error-msg').text('The token cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    });

    $('#country').on('blur',function(){
        var country    = $(this).val();
        if(country === ''){
            $('#error-msg').text('The country cannot be empty');
        }
    });

    $('#state').on('blur',function(){
        var state    = $(this).val();
        if(state === ''){
            $('#error-msg').text('The state cannot be empty');
        }
    });
    $('#city').on('blur',function(){
        var city    = $(this).val();
        if(city === ''){
            $('#error-msg').text('The city cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    });

    $('#address1').on('blur',function(){
        var address1    = $(this).val();
        if(address1 === ''){
            $('#error-msg').text('The address1 cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    });

    $('#address2').on('blur',function(){
        var address2    = $(this).val();
        if(address2 === ''){
            $('#error-msg').text('The address2 cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    });

    $('#zip').on('blur',function(){
        var zip    = $(this).val();
        if(zip === ''){
            $('#error-msg').text('The zip cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    });

    $('#phone').on('blur',function(){
        var phone    = $(this).val();
        if(phone === ''){
            $('#error-msg').text('The phone cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    });

    $('#favcategory').on('blur',function(){
        var favcategory    = $(this).val();
        if(favcategory === 'not'){
            $('#error-msg').text('The favourite category cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    });
});

function checkEmpty(){
    // var username            = $('#username').val(''),
    //     passwor             = $('#passwor').val(''),
    //     rePeatePassword     = $('#rePeatePassword').val(''),
    //     firstname           = $('#firstname').val(''),
    //     lastname            = $('#lastname').val(''),
    //     email               = $('#email').val(''),
    //     token               = $('#token').val(''),
    //     country             = $('#country').val(''),
    //     state               = $('#state').val(''),
    //     city                = $('#city').val(''),
    //     address1            = $('#address1').val(''),
    //     address2            = $('#address2').val(''),
    //     zip                 = $('#zip').val(''),
    //     phone               = $('#phone').val(''),
    //     favcategory         = $('#favcategory').val('');
    //

}